/************************************************************************
Purpose: 	Create Plant-Period Panel
*************************************************************************/

set more off
clear all
pause on

********************************************************************************
*** Create Plant-Period Panel
********************************************************************************

use "$TRADING_DATA_CLEAN/panel_plant-period-date.dta", clear
keep gpcb_id commodity period date bid_day bid_day_norm *_daily_*

local sides buy sell
local vars bid_qty bid_value trade_qty trade_value

foreach s of local sides{
	foreach v of local vars{
		bysort gpcb_id commodity: egen temp = sum(`s'_`v'_daily_sum)
		bysort gpcb_id commodity: egen `s'_`v'_period_sum = mean(temp)
		drop temp
	}
	gen `s'_bid_price_period_avg = `s'_bid_value_period_sum / `s'_bid_qty_period_sum 
	gen `s'_trade_price_period_avg = `s'_trade_value_period_sum / `s'_trade_qty_period_sum
}

label var buy_bid_qty_period_sum "Total quantity of purchase bids placed by period (kg)"
label var buy_bid_price_period_avg "Average price of purchase bids placed by period (Rs/kg) = buy_bid_value_period_sum / buy_bid_qty_period_sum"
label var buy_bid_value_period_sum "Total value of purchase bids placed by period (Rs)"
label var sell_bid_qty_period_sum "Total quantity of sale bids placed by period (kg)"
label var sell_bid_price_period_avg "Average price of sale bids placed by period (Rs/kg) = sell_bid_value_period_sum / sell_bid_qty_period_sum"
label var sell_bid_value_period_sum "Total value of sale bids placed by period (Rs)"
label var buy_trade_qty_period_sum "Total quantity of permits purchased by period (kg)"
label var buy_trade_price_period_avg "Average price of permits purchased by period (Rs/kg) = buy_trade_value_period_sum / buy_trade_qty_period_sum"
label var buy_trade_value_period_sum "Total value of permits purchased placed by period (Rs)"
label var sell_trade_qty_period_sum "Total quantity of permits sold by period (kg)"
label var sell_trade_price_period_avg "Average price of permits sold by period (Rs/kg) = sell_trade_value_period_sum / sell_trade_qty_period_sum"
label var sell_trade_value_period_sum "Total value of permits sold placed period day (Rs)"

drop date bid_day bid_day_norm *_daily_*
sort gpcb_id period 
duplicates drop
drop if missing(gpcb_id)

rename period commodity_period
merge 1:1 gpcb_id commodity_period using "$TRADING_DATA_OUT/inventory_plant-commodity.dta"
drop _merge
sort gpcb_id commodity_period 
foreach var of varlist *_period_* {
	replace `var' = 0 if missing(`var')
}

********************************************************************************
*** Check Inconsistency 
********************************************************************************

preserve 
gen my_final_inventory = allocated_quantity + buy_trade_qty_period_sum + sell_trade_qty_period_sum - consumed_period
keep gpcb_id commodity commodity_period buy_trade_qty_period_sum purchased_period sell_trade_qty_period_sum sold_period my_final_inventory final_inventory_period
order gpcb_id commodity commodity_period buy_trade_qty_period_sum purchased_period sell_trade_qty_period_sum sold_period my_final_inventory final_inventory_period
drop if (buy_trade_qty_period_sum == purchased_period) & (sell_trade_qty_period_sum == -sold_period) & (my_final_inventory == final_inventory_period)
sort commodity_period gpcb_id
// br
* 225 / 1560 plant-period observations are inconsistent.
* This should be the same as in clean_trading_data.do
restore

********************************************************************************
*** Prepare Data for Structural Model 
********************************************************************************

rename commodity_period period
merge m:1 period using "$TRADING_DATA_OUT/index_period.dta"
drop _merge

* Rename variables
rename period period_num
rename allocated_quantity permit_alloc
rename final_inventory_period permit_inventory
rename consumed_period emission_val
rename purchased_period permit_purchased 
rename sold_period permit_sold
rename buy_trade_price_period_avg price_permit_trade_purchase
rename sell_trade_price_period_avg price_permit_trade_sale
rename buy_bid_price_period_avg price_permit_bid_purchase
rename sell_bid_price_period_avg price_permit_bid_sale
rename period_end_commodity period_end
rename period_end_compliance compliance_end
rename compliance_month_cap period_cap_prorated

* Generate new variables
gen permit_hold = permit_inventory + emission_val
gen permit_trade = permit_hold - permit_alloc
gen permit_trade_2 = permit_purchased - permit_sold
gen D_non_compliance = (permit_inventory < 0)
gen price_edc = 200
gen amount_edc = 0
replace amount_edc = price_edc * permit_inventory if D_non_compliance == 1

label var permit_hold "Total permits holdings (kg)"
label var permit_trade "Total permits traded (kg) = permits holdings -  permits allocation"
label var permit_trade_2 "Total permits traded (kg) = permits purchased - permits sold"
label var D_non_compliance "=1 if not compliant"
label var price_edc "EDC price (Rs/kg)"
label var amount_edc "EDC amount (Rs)" 

* Generate pro-rated version
foreach var of varlist permit_alloc permit_inventory permit_hold permit_trade permit_purchased permit_sold emission_val amount_edc {
	gen `var'_prorated = (`var' / compliance_length) * 30
}
* When permits are allocated, the expected period length was 31 days.
replace permit_alloc_prorated = permit_alloc / 31 * 30 if period_num == 6 

label var emission_val_prorated "Validate total emissions (monthly pro-rated kg)"
label var permit_alloc_prorated "Permit allocation (monthly pro-rated kg)"
label var permit_hold_prorated "Total permit holdings, end of period (monthly pro-rated kg)"
label var permit_inventory_prorated "Final permit holdings, end of period (monthly pro-rated kg)"
label var permit_trade_prorated "Net permit purchases/sales (monthly pro-rated kg)"
label var permit_purchased_prorated "Gross purchases of permits (monthly pro-rated kg)" 
label var permit_sold_prorated "Gross sales of permits (monthly pro-rated kg)" 
label var amount_edc_prorated "EDC amount (monthly pro-rated Rs)" 

* Set zero price to missing
foreach var of varlist price_permit_trade_purchase price_permit_trade_sale price_permit_bid_purchase price_permit_bid_sale{
	replace `var' = . if `var' == 0
}

keep gpcb_id period_num commodity period_start period_end period_length compliance_end compliance_length period_cap_prorated emission_val_prorated permit_alloc_prorated permit_hold_prorated permit_inventory_prorated permit_trade_prorated permit_purchased_prorated permit_sold_prorated price_permit_trade_purchase price_permit_trade_sale price_permit_bid_purchase price_permit_bid_sale D_non_compliance price_edc amount_edc_prorated
order gpcb_id period_num period_start period_end period_length compliance_end compliance_length period_cap_prorated emission_val_prorated permit_alloc_prorated permit_hold_prorated permit_inventory_prorated permit_trade_prorated permit_purchased_prorated permit_sold_prorated price_permit_trade_purchase price_permit_trade_sale price_permit_bid_purchase price_permit_bid_sale D_non_compliance price_edc amount_edc_prorated
sort gpcb_id period_num

// br if (permit_purchased > 0 & price_permit_trade_purchase == .) | (permit_sold > 0 & price_permit_trade_sale == .) | ///
// 	(permit_purchased == 0 & price_permit_trade_purchase != .) | (permit_sold == 0 & price_permit_trade_sale != .)
* 59 / 1560 plant-period observations are problematic.

save "$TRADING_DATA_CLEAN/panel_plant-period.dta", replace
